#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

//#include <ncurses.h>
//#include <menu.h>

//#include "terminal_stdscr.h"
#include "../ec_it8733_inc/ec_it8733_dev.h"


#include "ec_it8733_rw.h"

//#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

static const unsigned char const_u8LdnEnvControl = 0x04;
static const unsigned char const_u8LdnParallelPort = 0x03;

CEcIt8733Rw* CEcIt8733Rw::m_pInstance = NULL; 

CEcIt8733Rw* CEcIt8733Rw::getInstance()
{
   if (!m_pInstance)   // Only allow one instance of class to be generated.
      m_pInstance = new CEcIt8733Rw;
   return m_pInstance;
}


CEcIt8733Rw::CEcIt8733Rw()
{
  m_u8PwmDefaultValue = 0;
  m_fd=-1;
	
	m_fd = open("/dev/ec_it8733", O_RDWR);
	if (m_fd < 0)
	{
		dbg_rn_prt("can't open!\n");
	}
  m_u16BaseAddrLdn04 = 0x0290;
  m_u16BaseAddrLdnParallelPort = 0;
}

CEcIt8733Rw::~CEcIt8733Rw()
{
	if(m_fd != -1)
    close(m_fd);
  m_fd=-1;
}

unsigned char CEcIt8733Rw::ec_inb( unsigned short int port)
{
  return dev_inb( m_fd, port );
}
void CEcIt8733Rw::ec_outb( unsigned char value, unsigned short int port)
{
  dev_outb( m_fd, value, port );
}

void CEcIt8733Rw::EnterConfigMode ()
{
  UINT8  PnpKey;

  PnpKey = 0x87;
  ec_outb ( PnpKey, SIO_CONFIG_PORT);
  PnpKey = 0x01;
  ec_outb ( PnpKey, SIO_CONFIG_PORT);
  PnpKey = 0x55;
  ec_outb ( PnpKey, SIO_CONFIG_PORT);
  PnpKey = 0x55;
  ec_outb ( PnpKey, SIO_CONFIG_PORT);
  
  return;
}

void CEcIt8733Rw::ExitConfigMode ()
{
  UINT8  RegData = 0x00;

  ec_outb ( SIO_CONFIG_CTRL,LPC_EC_INDEX_PORT);
  RegData = ec_inb ( LPC_EC_DATA_PORT) | BIT1;
  ec_outb ( SIO_CONFIG_CTRL,LPC_EC_INDEX_PORT);  
  ec_outb ( RegData,LPC_EC_DATA_PORT);
  
  return;
}


ssize_t CEcIt8733Rw::ec_it8733_dev_config( unsigned char *Chipid0, unsigned char *Chipid1)
{
  //int i;
	//unsigned char Chipid0,Chipid1 =0;
	UINT16  DataTemp;
  EnterConfigMode();
  EnterConfigMode();
  ec_outb ( 0x20,LPC_EC_INDEX_PORT);  
  *Chipid0 = ec_inb (LPC_EC_DATA_PORT);
  //dbg_rn_prt("Chipid0 = %02x \n",Chipid0);

  //ecram_vals[0]=Chipid0;

  ec_outb ( 0x21,LPC_EC_INDEX_PORT);
  *Chipid1 = ec_inb (LPC_EC_DATA_PORT); 
  //dbg_rn_prt("Chipid1 = %02x \n",Chipid1);

  ec_outb ( 0x22,LPC_EC_INDEX_PORT);

  DataTemp = ec_inb (LPC_EC_DATA_PORT);
  //dbg_rn_prt("DataTemp = %02x \n",DataTemp);
  ExitConfigMode ();
  return 0;
}

ssize_t CEcIt8733Rw::ec_it8733_pnp_read_baseaddr(unsigned char u8LDN, unsigned short *u16BaseAddrPara)
{
  unsigned char u8BaseAddrLsb;
  unsigned char u8BaseAddrMsb;
  unsigned char u8Val;
   

  ec_outb ( 0x07,LPC_EC_INDEX_PORT);
  ec_outb ( u8LDN,LPC_EC_DATA_PORT);
  

  ec_outb ( 0x30,LPC_EC_INDEX_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);  
  dbg_rn_prt( "0x30 = 0x%02X",u8Val );

#if 0
  ec_outb ( 0x62,LPC_EC_INDEX_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);  
  dbg_rn_prt( "0x62 = 0x%02X",u8Val );

  ec_outb ( 0x63,LPC_EC_INDEX_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);  
  dbg_rn_prt( "0x63 = 0x%02X",u8Val );

  ec_outb ( 0x70,LPC_EC_INDEX_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);  
  dbg_rn_prt( "0x70 = 0x%02X",u8Val );

  ec_outb ( 0x74,LPC_EC_INDEX_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);  
  dbg_rn_prt( "0x74 = 0x%02X",u8Val );

  ec_outb ( 0xF0,LPC_EC_INDEX_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);  
  dbg_rn_prt( "0xF0 = 0x%02X",u8Val );
#endif

#if 1
  ec_outb ( 0x30,LPC_EC_INDEX_PORT);
  //ec_outb (0x01, LPC_EC_DATA_PORT);  
 
  ec_outb ( 0x30,LPC_EC_INDEX_PORT);
  u8Val =  ec_inb ( LPC_EC_DATA_PORT);  
  dbg_rn_prt ("gg 0x30 = 0x%02X",u8Val );
#endif 

  ec_outb ( 0x60,LPC_EC_INDEX_PORT);
  u8BaseAddrMsb = ec_inb (LPC_EC_DATA_PORT);
  ec_outb ( 0x61,LPC_EC_INDEX_PORT);
  u8BaseAddrLsb = ec_inb (LPC_EC_DATA_PORT);

  unsigned short u16BaseAddr = u8BaseAddrMsb;
  u16BaseAddr = (u16BaseAddr<<8)+u8BaseAddrLsb;

  *u16BaseAddrPara = u16BaseAddr;
  //*u16BaseAddrPara = u8BaseAddrLsb;

#if 0
  // for parallel port modes spp epp ecp
  ec_outb ( 0xF0,LPC_EC_INDEX_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);
  dbg_rn_prt ("parallel port modes spp epp ecp 0xF0 = 0x%02X",u8Val );
  ec_outb ( u8Val&0xFC,LPC_EC_DATA_PORT);
  u8Val = ec_inb (LPC_EC_DATA_PORT);
  dbg_rn_prt ("parallel port modes spp epp ecp 0xF0 = 0x%02X",u8Val );
#endif

  return 0;
}

#if 0
static unsigned char g_u8Pwm=15;
static bool g_blPwm;
#endif

#if 0
void CEcIt8733Rw::entry_test_status(unsigned char u8Ldn)
{
  unsigned char u8Val;
#if 1
  EnterConfigMode();
  EnterConfigMode();

  unsigned short u16BaseAddr;
  ec_it8733_pnp_read_baseaddr( u8Ldn, &u16BaseAddr);
  m_u16BaseAddrLdn04 = u16BaseAddr;

  ExitConfigMode ();
#endif

  u16BaseAddr = m_u16BaseAddrLdn04;
  dbg_rn_prt( "u16BaseAddr = %08X\n",u16BaseAddr );

  ec_outb ( 0x00,u16BaseAddr + 0x05);
  //ec_outb ( 0x80,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x00 = %02X",u8Val);

#if 0
  ec_outb ( 0x00,u16BaseAddr + 0x05);
  ec_outb ( 0x80,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x00 = %02X",u8Val);

  ec_outb ( 0x00,u16BaseAddr + 0x05);
  ec_outb ( 0x19,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x00 = %02X",u8Val);
#endif

#if 0
  ec_outb ( 0x00,u16BaseAddr + 0x05);
  //ec_outb ( 0x19,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x00 = %02X",u8Val);

  ec_outb ( 0x00,u16BaseAddr + 0x05);
  ec_outb ( 0x59,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x00 = %02X",u8Val);


  ec_outb ( 0x0C,u16BaseAddr + 0x05);
  //ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x0C = %02X",u8Val);

  ec_outb ( 0x0C,u16BaseAddr + 0x05);
  ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x0C = %02X",u8Val);

#endif

#if 1
  ec_outb ( 0x13,u16BaseAddr + 0x05);
  //ec_outb ( 0x77,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x13 = %02X",u8Val);
#endif

  ec_outb ( 0x14,u16BaseAddr + 0x05);
  //ec_outb ( 0x8F,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x14 = %02X",u8Val);

  ec_outb ( 0x15,u16BaseAddr + 0x05);
  //ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);


  if(m_u8PwmDefaultValue==0)
  {
  ec_outb ( 0x63,u16BaseAddr + 0x05);
  //ec_outb ( 0x74,u16BaseAddr + 0x06);  //bios default value
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  m_u8PwmDefaultValue=u8Val;
  dbg_rn_prt("0x63 = %02X",u8Val);  
  }

  ec_outb ( 0x63,u16BaseAddr + 0x05);
  ec_outb ( m_u8PwmDefaultValue,u16BaseAddr + 0x06);  //bios default value
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x63 = %02X",u8Val);  

  ec_outb ( 0x15,u16BaseAddr + 0x05);
  ec_outb ( 0x85,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);

  ec_outb ( 0x16,u16BaseAddr + 0x05);
  //ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x16 = %02X",u8Val);

  ec_outb ( 0x17,u16BaseAddr + 0x05);
  //ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x17 = %02X",u8Val);

#if 0
  ec_outb ( 0x15,u16BaseAddr + 0x05);
  ec_outb ( 0x85,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);

  ec_outb ( 0x15,u16BaseAddr + 0x05);
  //ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);

  ec_outb ( 0x15,u16BaseAddr + 0x05);
  ec_outb ( 0x05,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);
#endif

#if 1
  ec_outb ( 0x51,u16BaseAddr + 0x05);
  //ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x51 = %02X",u8Val);
#endif


  ec_outb ( 0x64,u16BaseAddr + 0x05);
  //ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x64 = %02X",u8Val);


  //[feature]in Closed-loop Mode set default value from bios to control cpu fan rpm enable fan smoothing
  ec_outb ( 0x64,u16BaseAddr + 0x05);
  ec_outb ( u8Val | 0x80,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x64 = %02X",u8Val);


  {
    int i;
    for(i=0x60;i<=0x6F;i++)
    {
      ec_outb ( i,u16BaseAddr + 0x05);
      //ec_outb ( 0x07,u16BaseAddr + 0x06);
      u8Val = ec_inb ( u16BaseAddr + 0x06);
      dbg_rn_prt("0x%02X = %02X",i,u8Val);

    }

  }

#if 0
  ec_outb ( 0x00,u16BaseAddr + 0x05);
  ec_outb ( 0x80,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x00 = %02X",u8Val);



  ec_outb ( 0x13,u16BaseAddr + 0x05);
  ec_outb ( 0x77,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x13 = %02X",u8Val);

  ec_outb ( 0x14,u16BaseAddr + 0x05);
  ec_outb ( 0x8F,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x14 = %02X",u8Val);

  ec_outb ( 0x15,u16BaseAddr + 0x05);
  ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);

  ec_outb ( 0x16,u16BaseAddr + 0x05);
  ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x16 = %02X",u8Val);

  ec_outb ( 0x17,u16BaseAddr + 0x05);
  ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x17 = %02X",u8Val);

  ec_outb ( 0x0C,u16BaseAddr + 0x05);
  ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x0C = %02X",u8Val);

  ec_outb ( 0x00,u16BaseAddr + 0x05);
  ec_outb ( 0x19,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x00 = %02X",u8Val);


#if 1
  ec_outb ( 0x51,u16BaseAddr + 0x05);
  ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x51 = %02X",u8Val);
#endif

#endif

}
#endif

ssize_t CEcIt8733Rw::ec_it8733_start_read_cpu_temp(unsigned char *u8FanPwm, int *nCpuTemp, int *nMainBoardTemp, int *nChassisTemp, int *nCpuFan )
{
  int i;
   
  unsigned char u8Val;
  //entry_test_status( const_u8LdnEnvControl );

  unsigned short u16BaseAddr;
  get_baseaddr_env_control( &u16BaseAddr );

  m_u16BaseAddrLdn04 = u16BaseAddr;

  ec_outb ( 0x63,u16BaseAddr + 0x05);
  //ec_outb ( 0x90,u16BaseAddr + 0x06);
  //ec_outb ( u8FanPwm,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  *u8FanPwm = u8Val;
  dbg_rn_prt("0x63 = %02X",u8Val);

  ec_outb ( 0x29,u16BaseAddr + 0x05);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x29 = %4d",u8Val);
  *nMainBoardTemp=u8Val;

  ec_outb ( 0x2A,u16BaseAddr + 0x05);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x2A = %4d",u8Val);
  *nCpuTemp=u8Val;

  ec_outb ( 0x2B,u16BaseAddr + 0x05);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x2B = %4d",u8Val);
  *nChassisTemp=u8Val;

  for(i=0;i<1;i++)
  {
    int nFanVal; 
    int nFanLsb, nFanMsb;
    ec_outb ( 0x0D+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x0D+i,u8Val);
    nFanLsb = u8Val;

    ec_outb ( 0x18+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x18+i,u8Val);

    nFanMsb = u8Val;
    nFanMsb = (nFanMsb<<8) + nFanLsb;
    

    nFanVal = 1.35 * 1000000 / ( nFanMsb * 2);
    dbg_rn_prt("nFanVal = %8d",nFanVal);
    *nCpuFan=nFanVal;


  }

	return 0;
}

ssize_t CEcIt8733Rw::ec_it8733_read_cpu_temp(unsigned char *u8FanPwm, int *nCpuTemp, int *nMainBoardTemp, int *nChassisTemp, int *nCpuFan )
{
  int i;
   
  unsigned char u8Val;
  //entry_test_status();

   unsigned short u16BaseAddr = m_u16BaseAddrLdn04;

  ec_outb ( 0x63,u16BaseAddr + 0x05);
  //ec_outb ( 0x90,u16BaseAddr + 0x06);
  //ec_outb ( u8FanPwm,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  *u8FanPwm = u8Val;
  dbg_rn_prt("0x63 = %02X",u8Val);

#if 0
  ec_outb ( 0x51,u16BaseAddr + 0x05);
  ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x51 = %02X",u8Val);
#endif

  ec_outb ( 0x29,u16BaseAddr + 0x05);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x29 = %4d",u8Val);
  *nMainBoardTemp=u8Val;

  ec_outb ( 0x2A,u16BaseAddr + 0x05);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x2A = %4d",u8Val);
  *nCpuTemp=u8Val;

  ec_outb ( 0x2B,u16BaseAddr + 0x05);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x2B = %4d",u8Val);
  *nChassisTemp=u8Val;

  for(i=0;i<1;i++)
  {
    int nFanVal; 
    int nFanLsb, nFanMsb;
    ec_outb ( 0x0D+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x0D+i,u8Val);
    nFanLsb = u8Val;

    ec_outb ( 0x18+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x18+i,u8Val);

    nFanMsb = u8Val;
    nFanMsb = (nFanMsb<<8) + nFanLsb;
    

    nFanVal = 1.35 * 1000000 / ( nFanMsb * 2);
    dbg_rn_prt("nFanVal = %8d",nFanVal);
    *nCpuFan=nFanVal;


  }

	return 0;
}


ssize_t CEcIt8733Rw::ec_it8733_set_fan_pwm(unsigned char u8FanPwm )
{
  int i;
   
  unsigned char u8Val;

   unsigned short u16BaseAddr = m_u16BaseAddrLdn04;
  dbg_rn_prt( "u16BaseAddr = %08X\n",u16BaseAddr );

  ec_outb ( 0x15,u16BaseAddr + 0x05);
  //ec_outb ( 0x00,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);

  ec_outb ( 0x15,u16BaseAddr + 0x05);
  ec_outb ( 0x05,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x15 = %02X",u8Val);

  ec_outb ( 0x64,u16BaseAddr + 0x05);
  //ec_outb ( 0x07,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x64 = %02X",u8Val);

  //[feature]in Closed-loop Mode set default value from bios to control cpu fan rpm enable fan smoothing
  ec_outb ( 0x64,u16BaseAddr + 0x05);
  ec_outb ( u8Val & 0x7F,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x64 = %02X",u8Val);



  ec_outb ( 0x63,u16BaseAddr + 0x05);
  ec_outb ( u8FanPwm,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  dbg_rn_prt("0x63 = %02X",u8Val);

  for(i=0;i<1;i++)
  {
    int nFanVal; 
    int nFanLsb, nFanMsb;
    ec_outb ( 0x0D+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x0D+i,u8Val);
    nFanLsb = u8Val;

    ec_outb ( 0x18+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x18+i,u8Val);

    nFanMsb = u8Val;
    nFanMsb = (nFanMsb<<8) + nFanLsb;
    

    nFanVal = 1.35 * 1000000 / ( nFanMsb * 2);
    dbg_rn_prt("nFanVal = %8d",nFanVal);
    //*nCpuFan=nFanVal;


  }

	return 0;
}

ssize_t CEcIt8733Rw::ec_it8733_get_fan_pwm(unsigned char *u8FanPwm )
{
  int i;
   
  unsigned char u8Val;


   unsigned short u16BaseAddr = m_u16BaseAddrLdn04;
  dbg_rn_prt( "u16BaseAddr = %08X\n",u16BaseAddr );


  ec_outb ( 0x63,u16BaseAddr + 0x05);
  //ec_outb ( u8FanPwm,u16BaseAddr + 0x06);
  u8Val = ec_inb ( u16BaseAddr + 0x06);
  *u8FanPwm = u8Val;
  dbg_rn_prt("0x63 = %02X",u8Val);


  for(i=0;i<1;i++)
  {
    int nFanVal; 
    int nFanLsb, nFanMsb;
    ec_outb ( 0x0D+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x0D+i,u8Val);
    nFanLsb = u8Val;

    ec_outb ( 0x18+i,u16BaseAddr + 0x05);
    u8Val = ec_inb ( u16BaseAddr + 0x06);
    dbg_rn_prt("0x%02X = %02X",0x18+i,u8Val);

    nFanMsb = u8Val;
    nFanMsb = (nFanMsb<<8) + nFanLsb;
    

    nFanVal = 1.35 * 1000000 / ( nFanMsb * 2);
    dbg_rn_prt("nFanVal = %8d",nFanVal);
    //*nCpuFan=nFanVal;


  }

	return 0;
}




int CEcIt8733Rw::get_chip_id(unsigned char *chipid0, unsigned char *chipid1)
{


	ec_it8733_dev_config(chipid0,chipid1);


	return 0;

}

int CEcIt8733Rw::get_baseaddr_from_pnp(  unsigned char u8Ldn, unsigned short *u16FBaseAddrPara )
{
#if 1
  EnterConfigMode();
  EnterConfigMode();

  unsigned short u16BaseAddr;
  ec_it8733_pnp_read_baseaddr( u8Ldn, &u16BaseAddr);
  *u16FBaseAddrPara = u16BaseAddr;

  ExitConfigMode ();
#endif
  return 0;

}

int CEcIt8733Rw::get_baseaddr_env_control( unsigned short *u16FBaseAddrPara )
{
  unsigned short u16BaseAddr;
  get_baseaddr_from_pnp( const_u8LdnEnvControl, &u16BaseAddr);
  m_u16BaseAddrLdn04 = u16BaseAddr;
  *u16FBaseAddrPara = m_u16BaseAddrLdn04;
  return 0;
}

int CEcIt8733Rw::get_baseaddr_parallel_port( unsigned short *u16FBaseAddrPara )
{
  unsigned short u16BaseAddr;
  get_baseaddr_from_pnp( const_u8LdnParallelPort, &u16BaseAddr);
  m_u16BaseAddrLdnParallelPort = u16BaseAddr;
  *u16FBaseAddrPara = m_u16BaseAddrLdnParallelPort;

  //get_para_port_reg();
  return 0;
}

ssize_t CEcIt8733Rw::get_para_port_reg(unsigned char u8AddrOff, unsigned char* u8Data)
{
  int i; 
  unsigned char u8Val;

  unsigned short u16BaseAddr = m_u16BaseAddrLdnParallelPort+u8AddrOff;
  //dbg_rn_prt( "u16BaseAddr = 0x%04X\n",u16BaseAddr );

  u8Val = ec_inb (u16BaseAddr);
  dbg_rn_prt("u16BaseAddr:0x%04X = 0x%02X",u16BaseAddr,u8Val);
  *u8Data=u8Val;

	return 0;
}

ssize_t CEcIt8733Rw::set_para_port_reg(unsigned char u8AddrOff, unsigned char u8Data)
{
  int i; 
  unsigned char u8Val;

  unsigned short u16BaseAddr = m_u16BaseAddrLdnParallelPort+u8AddrOff;
  //dbg_rn_prt( "u16BaseAddr = 0x%04X\n",u16BaseAddr );

  ec_outb (u8Data,u16BaseAddr);
  //dbg_rn_prt("u16BaseAddr:0x%04X = 0x%02X",u16BaseAddr,u8Val);
  //*u8Data=u8Val;

	return 0;
}
